﻿
using System.Windows;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System;
using System.Windows.Controls;
namespace UIKit.Controls
{
    public class ImageWallControl : ImageWallContainer
    {
        #region Event
        public event EventHandler PulledDown;
        public event EventHandler PulledUp;
        public event SelectionChangedEventHandler SelectionChanged;
        #endregion

        #region DependencyProperties

        #region ColumnCount

        public int ColumnCount
        {
            get { return (int)GetValue(ColumnCountProperty); }
            set { SetValue(ColumnCountProperty, value); }
        }

        public static readonly DependencyProperty ColumnCountProperty =
            DependencyProperty.Register("ColumnCount", typeof(int), typeof(ImageWallContainer), new PropertyMetadata(OnColumnCountChanged));

        private static void OnColumnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int columnCount = (int)e.NewValue;
            ImageWallControl ctrl = (ImageWallControl)d;
            ctrl.OnColumnCountChanged(columnCount);
        }
        #endregion

        #region ColumnMargin

        public double ColumnMargin
        {
            get { return (double)GetValue(ColumnMarginProperty); }
            set { SetValue(ColumnMarginProperty, value); }
        }

        public static readonly DependencyProperty ColumnMarginProperty =
            DependencyProperty.Register("ColumnMargin", typeof(double), typeof(ImageWallContainer), new PropertyMetadata(OnColumnMarginChanged));


        private static void OnColumnMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageWallControl ctrl = (ImageWallControl)d;
            ctrl.OnColumnMarginChanged((double)e.NewValue, (double)e.OldValue);
        }

        public double ContainerWidth
        {
            get { return (double)GetValue(ContainerWidthProperty); }
            set { SetValue(ContainerWidthProperty, value); }
        }

        public static readonly DependencyProperty ContainerWidthProperty =
            DependencyProperty.Register("ContainerWidth", typeof(double), typeof(ImageWallControl), new PropertyMetadata(OnContainerWidthChanged));

        private static void OnContainerWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageWallControl ctrl = (ImageWallControl)d;
            ctrl.OnContainerWidthChanged((double)e.NewValue, (double)e.OldValue);
        }
        #endregion

        #region IsFat

        public bool IsFlat
        {
            get { return (bool)GetValue(IsFlatProperty); }
            set { SetValue(IsFlatProperty, value); }
        }

        public static readonly DependencyProperty IsFlatProperty =
            DependencyProperty.Register("IsFat", typeof(bool), typeof(ImageWallControl), new PropertyMetadata(OnIsFlatChanged));

        private static void OnIsFlatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #endregion

        #region Method
        private void OnColumnCountChanged(int columnCount)
        {
            _imageWallCaculator.ColumnCount = columnCount;
        }

        private void OnColumnMarginChanged(double newValue, double oldValue)
        {
            _imageWallCaculator.ColumnMargin = newValue;
        }

        private void OnContainerWidthChanged(double newValue, double oldValue)
        {
            _imageWallCaculator.Width = ContainerWidth;
        }
        #endregion

        #region Override
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _imageWallCaculator.ItemTemplate = this.ItemTemplate;
            _imageWallCaculator.InitPool();
            if (this.Items != null && Items.Count > 0)
            {
                foreach (IImageModel imageModel in Items)
                {
                    _imageWallCaculator.AddItem(imageModel);
                }
            }
        }

        #region VirtualData
        protected override void VirtualizingData(double verticalOffset)
        {
            List<IImageModel> removeItems = _imageWallCaculator.RemoveItems(verticalOffset);
            List<IImageModel> showItems = _imageWallCaculator.ShowItems(verticalOffset);
            Debug.WriteLine("removeCount{0},showCount{1}", removeItems.Count, showItems.Count);
        }
        #endregion

        #region ItemsOverride
        protected override void AddItems(IList oldItems, IList newItems, int oldStartingIndex, int newStartingIndex)
        {
            IEnumerator enumerator = newItems.GetEnumerator();
            while (enumerator.MoveNext())
            {
                IImageModel model = (IImageModel)enumerator.Current;
                _imageWallCaculator.AddItem(model);
            }
        }

        protected override void RemoveItems(IList oldItems, IList newItems, int oldStartingIndex, int newStartingIndex)
        {
            base.RemoveItems(oldItems, newItems, oldStartingIndex, newStartingIndex);
        }

        protected override void ReplaceItems(IList oldItems, IList newItems, int oldStartingIndex, int newStartingIndex)
        {
            base.ReplaceItems(oldItems, newItems, oldStartingIndex, newStartingIndex);
        }
        #endregion

        #region PullDownAndUp
        protected override void BeginPullUp()
        {
            //判断拉的距离显示顶部进度条
            if (this.IsFlat)
            {

            }
        }

        protected override void PullUpToTop()
        {
            //1.隐藏顶部加载进度条
            //2.判断底部是否要继续拉动
        }

        protected override void PullDownToBottom()
        {
            //根据到达底部的位置
            if (this.PulledDown != null)
            {
                this.PulledDown(this, EventArgs.Empty);
            }
        }
        #endregion

        #endregion
    }
}
